<template>
	<view >
		<view class="home">
			<swiper indicator-dots="true" circular class="swiper"  >
				<swiper-item  v-for="(item,index) in swipers" :key = "item.id">
					<image :src="item.img" mode="scaleToFill"></image>
				</swiper-item>
			</swiper>
		</view>
		<view class="nav">
			<view class="nav_item">
				<view class="iconfont icon-ziyuan">					
				</view>
				<text>澳哥超市</text>
			</view>
			<view class="nav_item">
				<view class="iconfont icon-guanyuwomen">					
				</view>
				<text>澳哥超市</text>
			</view>
			<view class="nav_item">
				<view class="iconfont icon-tupian">					
				</view>
				<text>澳哥超市</text>
			</view>
			<view class="nav_item">
				<view class="iconfont icon-shipin">					
				</view>
				<text>澳哥超市</text>
			</view>
		</view>
		<!-- 推荐商品 -->
		<view class="hot_goods">
			<view class="title">
				推荐商品
			</view>
			<view class="goods_list">
				<view class="goods_item" v-for="(item,index) in goods" :key="item.id">
					<image :src="item.img_url" ></image>
					<view class="price">
						<text>￥{{item.sell_price}}</text>
						<text>￥{{item.market_price}}</text>
					</view>
					<view class="name">
						{{item.title}}
					</view>
				</view>
			</view>
		</view>
		<view v-if="end" class="end">
			没有更多数据
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swipers:[],
				goods:[],
				page:1,
				pagesize:10,
				end:false
			}
		},
		onLoad() {
			this.getSwiper();
			this.getHotGoods();
		},
		methods: {
			// 获取轮播图的数据
			async getSwiper(){
				// 封装接口至util/api.jss
				const res = await this.$myRequest({
					url:'/api/getlunbo'
				})
				this.swipers = res.data.message
			},
			// 获取热门商品列表数据
			async getHotGoods(){
				const res = await this.$myRequest({
					url:'/api/getgoods?pageindex='+this.page
				})
				// console.log(res.data.message)
				this.goods = [...this.goods,...res.data.message]
				if(res.data.message.length<this.pagesize){
					uni.showToast({
						title:"已经是最后一夜了"
					})
					this.end = true
				}
				// console.log(this.goods)
			}
		
		},
		onReachBottom() {
			if(this.end == false){
				this.page += 1
				uni.showLoading({
					title:"加载中"
				})
				setTimeout(()=>{
					this.getHotGoods()
				},1500)
			}else{
				return;
			}
		}
	}
</script>

<style lang="scss">
	.swiper{
		width: 750rpx;
		height: 380rpx;
		image{
			height: 100%;
			width: 100%;
		}
	}
	.nav{
		display: flex;
		justify-content: center;
		align-items: center;
		.nav_item{
			flex: 1;
			text-align: center;
			view{
				width: 120rpx;
				height: 120rpx;
				background-color: $shop-color;
				border-radius: 50%;
				margin: 10rpx auto;
				line-height: 120rpx;
				color: #FFFFFF;
				font-size: 50rpx;
			}
			.icon-tupian{
				font-size: 45rpx;
			}
			text{
				font-size: 30rpx;
			}
		}
	}
	.hot_goods{
		background-color: #eee;
		overflow: hidden;
		margin-top: 40rpx;
		.title{
			height: 100rpx;
			line-height: 100rpx;
			color: $shop-color;
			text-align: center;
			letter-spacing: 40rpx;
			background: #FFFFFF;
			margin: 7rpx 0;
		}
		.goods_list{
			padding: 0 15rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			.goods_item{
				background-color: #FFFFFF;
				width: 355rpx;
				height: 560rpx;
				padding: 15rpx;
				box-sizing: border-box;
				margin: 10rpx 0;
				image{
					width:80%;
					height: 300rpx;
					display: block;
					margin: auto;
					margin-bottom: 20rpx;
				}
				.price{
					color: $shop-color;
					font-size: 35rpx;
					
					text:nth-child(2){
						color: #ccc;
						font-size: 28rpx;
						margin-left: 20rpx;
						text-decoration: line-through;
					}
				}
				.name{
					font-size: 28rpx;
					line-height: 50rpx;
					padding-top: 25rpx;
					padding-bottom: 10rpx;
				}
			}
		}
	}
	.end{
		text-align: center;
		background-color: #eee;
		height: 80rpx;
		line-height: 80rpx;
	}
</style>
